﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using ApiManage.Svc.Controllers;
using Rhonin.Common.Entity.Enum;
using Rhonin.Common.Entity.UserTicket;
using System;

namespace ApiManage.Svc.Filter
{
    /// <summary>
    /// 是否登录验证
    /// </summary>
    public class CheckLoginAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 重写方法执行前操作
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            UserTicket ticket = BaseController.Ticket;
            if (ticket == null || !ticket.IsIn)
            {
                context.Result = new ContentResult()
                {
                    StatusCode = (int)ResultStatus.NotLogin,
                    Content = "未登录"
                };
                return;
            }
            if (ticket.CUI.LoginAt < DateTime.Now.AddHours(-3))
            {
                context.Result = new ContentResult()
                {
                    StatusCode = (int)ResultStatus.NotLogin,
                    Content = "登录超时，请重新登录"
                };
                return;
            }
            base.OnActionExecuting(context);
        }

    }
}
